package cn.fastoo.web.backstage.web.controller.member;

import cn.fastoo.common.dao.member.MemberDao;
import cn.fastoo.common.entity.member.Member;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.web.backstage.param.LoginUser;
import cn.fastoo.web.backstage.utils.ShiroUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberDao dao;

    @RequestMapping
    public String index() {
        return "/member/index";
    }

    @RequestMapping("会员详细信息")
    public String detail() {
        return "/member/会员详细信息";
    }

    @RequestMapping("批量分配顾问")
    public String piliang() {
        return "/member/批量分配顾问";
    }

    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute Member newData, QuerySearch querySearch, String op) {
         
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData, ShiroUtils.getUserId(),ShiroUtils.getName());
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                Member oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                if("save".equals(op)){
                    oldData.setActivityLocation(newData.getActivityLocation());
                    oldData.setAddress(newData.getAddress());
                    oldData.setBirthday(newData.getBirthday());
                    oldData.setCollectTime(newData.getCollectTime());
                    oldData.setHandset(newData.getHandset());
                    oldData.setIsSchool(newData.getIsSchool());
                    oldData.setKeyGrade(newData.getKeyGrade());
                    oldData.setParent(newData.getParent());
                    oldData.setRemark(newData.getRemark());
                    oldData.setStatus(newData.getStatus());
                    oldData.setUsername(newData.getUsername());
                    oldData.setMemberGradeId(newData.getMemberGradeId());
                    oldData.setMarketingUserId(newData.getMarketingUserId());
                    oldData.setSalesUserId(newData.getSalesUserId());
                    oldData.setIdCard(newData.getIdCard());
                    oldData.setParentTwo(newData.getParentTwo());
                    oldData.setHandsetTwo(newData.getHandsetTwo());
                    oldData.setHeadUrl(newData.getHeadUrl());
                    oldData.setOriginNotes(newData.getOriginNotes());
                    oldData.setConsultantGetMan(newData.getConsultantGetMan());
                    oldData.setCollectPeople(newData.getCollectPeople());
                    oldData.setNationality(newData.getNationality());
                    oldData.setEnglishName(newData.getEnglishName());
                    oldData.setWechat(newData.getWechat());
                    oldData.setSex(newData.getSex());
                    oldData.setUpdateMan(ShiroUtils.getName());
                    oldData.setUpdateTime(new Date());
                    dao.update(oldData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.findPage(querySearch.getSearch(),ShiroUtils.getUserId()),querySearch);
    }


    @RequestMapping("info")
    @ResponseBody
    public String info(@RequestParam Long id){
        return JsonResult.success(dao.findById(id));
    }

    @RequestMapping("fenpei")
    @ResponseBody
    public String fenpei(@RequestParam String memberIds, @RequestParam Long guwenId, LoginUser loginUser){
        try {

            dao.fenpei(memberIds,guwenId);
            return JsonResult.success("ok");
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.error("服务器异常,请联系管理员");

    }

}
